package self.hoy.demo.grainmall.coupon.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import self.hoy.demo.common.ServiceProviderName;
import self.hoy.demo.common.utils.R;
import self.hoy.demo.grainmall.coupon.feign.OrderService;

import javax.annotation.Resource;

/**
 * @author hyh
 * @date 2020/7/5
 */
@RestController
@RequestMapping("/test")
public class TestFeignClientController {

    @Value("${env1}")
    private String env1;

    @Value("${env2}")
    private String env2;

    @Value("${env3}")
    private String env3;

    @Value("${env4}")
    private String env4;

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private OrderService orderService;

    @GetMapping("/config/env")
    public R env() {
        return new R().put("environment", env1 + " " + env2 + " " + env3 + " " + env4);
    }

    @GetMapping("/feign/get/{id}")
    public R feign(@PathVariable("id")Long id) {
        return orderService.info(id);
    }

    @GetMapping("/rest/get/{id}")
    public R rest(@PathVariable("id")Long id) {
        return restTemplate.getForObject(
                "http://"+ ServiceProviderName.ORDER + "/order/order/info/" + id, R.class);
    }

}
